package com.example.dllo.keepdemo.dynamic.attention;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseFragment;
import com.example.dllo.keepdemo.dynamic.bean.AttentionBean;
import com.example.dllo.keepdemo.greendao.DynamicAttention;
import com.example.dllo.keepdemo.greendao.DynamicAttentionDao;
import com.example.dllo.keepdemo.tools.DaoSingleton;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.VolleySingleton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 16/1/8.
 */
public class AttentionFragment extends BaseFragment {
    private ListView listView;
    private ListViewAdapter adapter;
    private DynamicAttentionDao dynamicAttentionDao = DaoSingleton.getInstance().getDynamicAttentionDao();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_attention,container,false);
        listView = (ListView) view.findViewById(R.id.listview_fragment_attention);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //去下划线
        listView.setDivider(null);
        initData();


    }

    private void initData() {
        String url = "https://api.gotokeep.com/v1.1/rankinglist/user/recommend";
        GsonRequest<AttentionBean> gsonRequest = new GsonRequest<AttentionBean>(url, AttentionBean.class, new Response.Listener<AttentionBean>() {
            @Override
            public void onResponse(AttentionBean response) {
                adapter = new ListViewAdapter(getActivity(),response,2);
                listView.setAdapter(adapter);
                if (null != dynamicAttentionDao.queryBuilder().list()) {
                    dynamicAttentionDao.deleteAll();
                    addData(response);
                } else {
                    addData(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                adapter = new ListViewAdapter(dynamicAttentionDao.queryBuilder().list(),getActivity(),1);
                listView.setAdapter(adapter);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjU4OTkwNiwiZXhwIjoxNDU1MTgxOTA2LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.4I6FpbJT1_1fe8Z51oHt6Mv5t-3zXGGYhI1rMe3U8gM");
                map.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                map.put("Host", "api.gotokeep.com");
                map.put("X-KEEP-VERSION", "2.8.1");
                map.put("X-KEEP-CHANNEL", "keep");
                return map;
            }
        };

        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

    private void addData(AttentionBean response) {
        List<DynamicAttention> dynamicAttentionlistView = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            DynamicAttention dynamicAttention = new DynamicAttention();
            dynamicAttention.setTitle(response.getData().get(i).getUser().getUsername());
            dynamicAttention.setImage1(response.getData().get(i).getEntries().get(0).getPhoto());
            dynamicAttention.setImage2(response.getData().get(i).getEntries().get(1).getPhoto());
            dynamicAttention.setImage3(response.getData().get(i).getEntries().get(2).getPhoto());
            dynamicAttentionlistView.add(dynamicAttention);
        }
        dynamicAttentionDao.insertInTx(dynamicAttentionlistView);
    }
}
